<?php

/**
 * This is the model class for table "zy_event".
 *
 * The followings are the available columns in table 'zy_event':
	 * @property integer $event_id
	 * @property string $event_title
	 * @property string $event_content
	 * @property string $event_code
	 * @property string $start_time
	 * @property string $end_time
	 * @property string $award_expiry_date
	 * @property integer $prize_qty
	 * @property integer $is_delete
	 * @property string $remark
	 * @property string $create_time
	 * @property string $create_user
	 * @property string $update_time
	 * @property string $update_user
 */
class Event extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Event the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_event';
	}
	public function primaryKey()
	{
		return 'event_id';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('prize_qty, is_delete', 'numerical', 'integerOnly'=>true),
			array('event_title,event_banael, event_content, event_code,user_id,url, remark, create_user, update_user', 'length', 'max'=>255),
			array('start_time, end_time, award_expiry_date, create_time, update_time,winning_email_cn,winning_email_en,winning_sms_cn,winning_sms_en', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('event_id, event_title,event_banael, event_content, event_code, start_time, end_time, award_expiry_date, prize_qty,Used_gift_number,user_id, url,winning_email_cn,winning_email_en,winning_sms_cn,winning_sms_en,is_delete, remark, create_time, create_user, update_time, update_user', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'event_id' =>Yii::t("Event","Event ID"),
			'event_title' =>Yii::t("Event","Event Title"),
			'event_banael' =>Yii::t("Event","Event Banael"),
			'event_content' =>Yii::t("Event","Event Content"),
			'event_code' =>Yii::t("Event","Event Code"),
			'start_time' =>Yii::t("Event","Start Time"),
			'end_time' =>Yii::t("Event","End Time"),
			'award_expiry_date' =>Yii::t("Event","Award Expiry Date"),
			'prize_qty' =>Yii::t("Event","Prize Qty"),
			'Used_gift_number'=>Yii::t("Event","Used Gift Number"),
			'user_id' =>Yii::t("Event","User Id"),
			'url' =>Yii::t("Event","Url"),
			'winning_email_cn' =>Yii::t("Event","Winning Email Cn"),
			'winning_email_en' =>Yii::t("Event","Winning Email En"),
			'winning_sms_cn' =>Yii::t("Event","Winning Sms Cn"),
			'winning_sms_en' =>Yii::t("Event","Winning Sms En"),
			'is_delete' =>Yii::t("Event","Is Delete"),
			'remark' =>Yii::t("Event","Remark"),
			'create_time' =>Yii::t("Event","Create Time"),
			'create_user' =>Yii::t("Event","Create User"),
			'update_time' =>Yii::t("Event","Update Time"),
			'update_user' =>Yii::t("Event","Update User"),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$level = yii::app()->user->level();
		$criteria=new CDbCriteria;

		$criteria->compare('event_id',$this->event_id);
		$criteria->compare('event_title',$this->event_title,true);
		$criteria->compare('event_banael',$this->event_banael,true);
		$criteria->compare('event_content',$this->event_content,true);
		$criteria->compare('event_code',$this->event_code,true);
		$criteria->compare('start_time',$this->start_time,true);
		$criteria->compare('end_time',$this->end_time,true);
		$criteria->compare('award_expiry_date',$this->award_expiry_date,true);
		$criteria->compare('prize_qty',$this->prize_qty);
		$criteria->compare('Used_gift_number',$this->prize_qty);
		if($level == 1){
		$criteria->compare('user_id',$this->user_id);
		}else{
			$criteria->compare('user_id',Yii::app()->user->id);
		}
		$criteria->compare('url',$this->url);
		$criteria->compare('winning_email_cn',$this->winning_email_cn);
		$criteria->compare('winning_email_en',$this->winning_email_en);
		$criteria->compare('winning_sms_cn',$this->winning_sms_cn);
		$criteria->compare('winning_sms_en',$this->winning_sms_en);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('create_user',$this->create_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(  
           		 'defaultOrder'=>'event_id DESC',  
       		 ),  
       		'pagination'=>array(  
           		 'pageSize'=>20  
       		 ),  
		));
	}
	
	//通过event的id找到title
	public function getEventTitle($event_id)
	{
		$model=Event::model()->findByPk($event_id);
		return $model->event_title;
	}
	//自动获取活动代码
	public function create_event_code(){
	$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
	for($i = 0; $i <6; $i++) {
		$event_code .= $pattern{mt_rand(0, 61)};
	}
	$sql="select event_code from zy_event where event_code ='".$event_code."'";
	$rs =Yii::app()->db->createCommand($sql)->queryAll();
	if ($rs) {
		create_event_code();
	}else{
		return $event_code;
	}
	}
	
	//查找合作商，level=9的用户
	public function getPartners()
	{
		$sql="select * from zy_user where level=9";
		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 return CHtml::listData($rs,'user_id','user_name');
	}
	 
	 //获取合作商的姓名
	 public function getPartnersName($user_id)
	 {
	 	$sql="select * from zy_user where user_id = '".$user_id."'";
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return $rs[0]['user_name'];
	 }		
	 
	 	 //使用递归的方法获取不重复密码
	 function getPasswrd(){
	 	//range 是将1到9 列成一个数组 
		$numbers = range (1,9); 
		//shuffle 将数组顺序随即打乱 
		shuffle($numbers); 
		//array_slice 取该数组中的某一段 
		$no=6; 
		$result = array_slice($numbers,0,$no);
		for ($i=0;$i<$no;$i++){
			$passwrd.= $result[$i];
		}
		$sql="select password from zy_event_gift_password where password ='".$passwrd."'";
		$rs = yii::app()->db->createCommand($sql)->queryAll();
		if(!empty($rs)){
			$passwrd = $this->getPasswrd();
			return $passwrd;
		}else{
			return $passwrd;
		}
	 }
	 
	 
	 	 //查询地址,返回list
	 function getAddressList($num){
	 
	 	$userId = yii::app()->user->id;
	 
	 	if($num == 'falg'){
	 
	 		$sql="select * from zy_award_address where belong = '".$userId."'";
	 
	 		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 
	 		return CHtml::listData($rs,'address_id','company_name');
	 
	 	}else{
	 
	 		$sql="select * from zy_award_address where address_id='".$num."' and  belong = '".$userId."'";
	 			
	 		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 
	 		return $rs[0]['address'];
	 
	 	}
	 }
	 
	 //admin 用户
	 function getAddressLists($num,$userId){
	 
	 		$sql="select * from zy_award_address where address_id='".$num."' and  belong = '".$userId."'";
	 			
	 		$rs = Yii::app()->db->createCommand($sql)->queryAll();
	 
	 		return $rs[0]['address'];
	 }
	 
	 //手动抽奖的限制
	 public function getCount($event_id)
	 {
	 	$sql="SELECT COUNT(*) FROM zy_event_gift_password WHERE event_id='$event_id' AND state IN(1,2,3)";
	 	$num = Yii::app()->db->createCommand($sql)->queryAll();
	 	return $num;
	 }
}